package com.zzl.finance.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzl.finance.order.constant.InvoiceCategoryEnum;
import com.zzl.finance.order.constant.InvoiceStatusEnum;
import com.zzl.finance.order.constant.InvoiceTypeEnum;
import com.zzl.finance.order.dao.OrderInvoiceDao;
import com.zzl.finance.order.dto.OrderInvoiceAddDto;
import com.zzl.finance.order.dto.OrderInvoiceTotalInfoDto;
import com.zzl.finance.order.dto.OrderInvoiceUpdateDto;
import com.zzl.finance.order.entity.OrderInvoiceEntity;
import com.zzl.finance.order.entity.OrderInvoiceRelationEntity;
import com.zzl.finance.order.service.*;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.serurity.entity.User;
import com.zzl.general.common.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author sunpp
 * @date 2022-03-07 10:41:16
 */
@Service("orderInvoiceService")
public class OrderInvoiceServiceImpl extends ServiceImpl<OrderInvoiceDao, OrderInvoiceEntity> implements OrderInvoiceService {


    @Autowired
    private OrderService orderService;

    @Autowired
    private EmailService emailService;
    @Autowired
    private MailAddressService mailAddressService;
    @Autowired
    private InvoiceTitleService invoiceTitleService;
    @Autowired
    private OrderInvoiceRelationService orderInvoiceRelationService;

    @Override
    public List<OrderInvoiceEntity> getOrderInvoice(String orderId) {
        OrderInvoiceRelationEntity orderInvoiceRelationEntity = new OrderInvoiceRelationEntity();
        QueryWrapper<OrderInvoiceRelationEntity> wrapper = new QueryWrapper<>();
        QueryWrapper<OrderInvoiceEntity> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda().eq(OrderInvoiceEntity::getOrderId, orderId);
        List<OrderInvoiceEntity> list = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        for (OrderInvoiceEntity entity : list) {
            entity.setInvoiceStatusStr(InvoiceStatusEnum.getValue(entity.getInvoiceStatus()));
            entity.setInvoiceCategoryStr(InvoiceCategoryEnum.getValue(entity.getInvoiceCategory()));
            entity.setInvoiceTypeStr(InvoiceTypeEnum.getValue(entity.getInvoiceType()));
        }
        return list;
    }

    /**
     * 分页查询
     *
     * @param params 查询参数
     * @param user
     * @return 分页数据
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, User user) {
        /*TODO*/
        return null;
    }

    /**
     * 新增
     *
     * @param orderInvoiceAddDto 新增实体
     * @param user               当前登录用户
     * @throws SysException 业务异常
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrderInvoice(OrderInvoiceAddDto orderInvoiceAddDto, User user) throws SysException {
        /*TODO*/
    }

    /**
     * 修改
     *
     * @param orderInvoiceUpdateDto 修改实体
     * @param user                  当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void updateOrderInvoiceById(OrderInvoiceUpdateDto orderInvoiceUpdateDto, User user) throws SysException {
        OrderInvoiceEntity orderInvoiceEntity = new OrderInvoiceEntity();
        BeanUtils.copyProperties(orderInvoiceUpdateDto, orderInvoiceEntity);
        orderInvoiceEntity.setUpdaterId(user.getId());
        if (!this.updateById(orderInvoiceEntity)) {
            throw new SysException("A00002", "修改失败！");
        }
    }

    @Override
    public void updateInvoiceStatus(OrderInvoiceUpdateDto orderInvoice, User user) throws SysException {
        UpdateWrapper<OrderInvoiceEntity> wrapper = new UpdateWrapper<>();
        wrapper.lambda().set(OrderInvoiceEntity::getInvoiceStatus, InvoiceStatusEnum.INVALID_VOIDED.getCode())
                .eq(OrderInvoiceEntity::getId, orderInvoice.getId());
        if (!this.update(wrapper)) {
            throw new SysException("A00002", "修改失败！");
        }
    }

    @Override
    public OrderInvoiceTotalInfoDto getOrderInvoiceTotalInfoDto(User user) {
        /*TODO*/
        return null;
    }

    @Override
    public OrderInvoiceEntity info(String id) {
        /*TODO*/
        return null;
    }

}